package org.dromara.myprogram.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.myprogram.utils.HttpGetJson;
import org.springframework.stereotype.Service;
import org.dromara.myprogram.domain.bo.MyUnionlottoBo;
import org.dromara.myprogram.domain.vo.MyUnionlottoVo;
import org.dromara.myprogram.domain.MyUnionlotto;
import org.dromara.myprogram.mapper.MyUnionlottoMapper;
import org.dromara.myprogram.service.IMyUnionlottoService;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 双色球Service业务层处理
 *
 * @author gjb
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class MyUnionlottoServiceImpl implements IMyUnionlottoService {

    private final MyUnionlottoMapper baseMapper;

    /**
     * 查询双色球
     */
    @Override
    public MyUnionlottoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询双色球列表
     */
    @Override
    public TableDataInfo<MyUnionlottoVo> queryPageList(MyUnionlottoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MyUnionlotto> lqw = buildQueryWrapper(bo);
        Page<MyUnionlottoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询双色球列表
     */
    @Override
    public List<MyUnionlottoVo> queryList(MyUnionlottoBo bo) {
        LambdaQueryWrapper<MyUnionlotto> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MyUnionlotto> buildQueryWrapper(MyUnionlottoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MyUnionlotto> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), MyUnionlotto::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), MyUnionlotto::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDate()), MyUnionlotto::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getWeek()), MyUnionlotto::getWeek, bo.getWeek());
        lqw.eq(StringUtils.isNotBlank(bo.getRed()), MyUnionlotto::getRed, bo.getRed());
        lqw.eq(StringUtils.isNotBlank(bo.getBlue()), MyUnionlotto::getBlue, bo.getBlue());
        return lqw;
    }

    /**
     * 新增双色球
     */
    @Override
    public Boolean insertByBo(MyUnionlottoBo bo) {
        MyUnionlotto add = MapstructUtils.convert(bo, MyUnionlotto.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改双色球
     */
    @Override
    public Boolean updateByBo(MyUnionlottoBo bo) {
        MyUnionlotto update = MapstructUtils.convert(bo, MyUnionlotto.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MyUnionlotto entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除双色球
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int getUnionlottos() {
        //java8遍历集合获取所有id
        List<Long> ids = baseMapper.selectList().stream().map(MyUnionlotto::getId).toList();
        //批量删除
        if (ids.size()>0){
            this.deleteWithValidByIds(ids, false);
        }

        try {
            List<MyUnionlotto> allUnionlotto = HttpGetJson.getAllUnionlotto(1, 10000);
            boolean b = this.baseMapper.insertBatch(allUnionlotto);
            return b ? 1 : 0;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<MyUnionlotto> findAllMyunionlottos() {
        return this.baseMapper.selectList();
    }
}
